package com.ylbh.modules.userclass.controller;

import java.util.*;

import com.ylbh.common.annotation.SysLog;
import com.ylbh.common.pojo.icon.dto.AddIconDTO;
import com.ylbh.common.pojo.icon.dto.IconDTO;
import com.ylbh.common.pojo.icon.vo.IconDetailsVO;
import com.ylbh.common.pojo.upload.vo.UploadItem;
import com.ylbh.common.pojo.userclass.dto.BusinessUserClassDTO;
import com.ylbh.common.utils.*;
import com.ylbh.modules.userclass.service.ShoppingUserClassService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;


/**
 * 联盟商家用户分类信息表
 *
 * @author czx
 * @date 2019-04-19 14:38:31
 */
@RestController
@RequestMapping("business/class")
public class ShoppingUserClassController {

    @Autowired
    private ShoppingUserClassService shoppingUserClassService;

    private String uploadHost = "http://image.yilianbaihui.cn/consumer/";


    @ApiOperation("联盟商家用户分类信息列表")
    @RequestMapping("/queryClassification")
    @RequiresPermissions("business:class:queryClassification")
    public CommonResult<PageUtils> queryClassification(@RequestParam(value = "page")int page,@RequestParam(value = "limit")int limit){
        PageUtils lists = shoppingUserClassService.queryClassification(page, limit);
        return Ref.ok(lists);
    }

    @SysLog("新增联盟商家用户分类信息")
    @ApiOperation("新增联盟商家用户分类信息")
    @RequestMapping("/addClass")
    @RequiresPermissions("business:class:addClass")
    public CommonResult<Void> addClass(@RequestBody BusinessUserClassDTO businessUserClassDTO){
        shoppingUserClassService.addClass(businessUserClassDTO.getClassname());
        return Ref.ok();
    }

    @ApiOperation("根据id修改联盟商家用户分类信息")
    @RequestMapping("/modifyByIds")
    @RequiresPermissions("business:class:modifyByIds")
    public CommonResult<Void> modifyByIds(@RequestBody BusinessUserClassDTO businessUserClassDTO){
        shoppingUserClassService.modifyByIds(businessUserClassDTO.getId(),businessUserClassDTO.getClassname());
        return Ref.ok();
    }

    @SysLog("根据id删除联盟商家用户分类信息")
    @ApiOperation("根据id删除联盟商家用户分类信息")
    @RequestMapping("/deleteByIds")
    @RequiresPermissions("business:class:deleteByIds")
    public CommonResult<Void> deleteByIds(@RequestBody String[] ids){
        shoppingUserClassService.deleteByIds(ids[0]);
        return Ref.ok();
    }

    @ApiOperation("根据id查看联盟商家用户分类信息")
    @RequestMapping("/getClassNameById/{id}")
    @RequiresPermissions("business:class:getClassNameById")
    public CommonResult<String> getClassNameById(@PathVariable Long id){
        String className = shoppingUserClassService.getClassNameById(id);
        return Ref.ok(className);
    }

    @ApiOperation("icon图标管理列表")
    @RequestMapping("/queryIconList")
    @RequiresPermissions("business:class:queryIconList")
    public CommonResult<PageUtils> queryIconList(PageDTO pageDTO){
        PageUtils list = shoppingUserClassService.queryIconList(pageDTO);
        return Ref.ok(list);
    }

    @ApiOperation("icon图标详情")
    @RequestMapping("/getIconById/{id}")
    @RequiresPermissions("business:class:getIconById")
    public CommonResult<IconDetailsVO> getIconById(@PathVariable(value = "id")Long id){
        IconDetailsVO info = shoppingUserClassService.getIconById(id);
        return Ref.ok(info);
    }

    @SysLog("icon图标修改")
    @ApiOperation("icon图标修改")
    @RequestMapping("/modifyIcon")
    @RequiresPermissions("business:class:modifyIcon")
    public CommonResult<Void> modifyIcon(@RequestBody IconDTO iconDTO){
        shoppingUserClassService.modifyIcon(iconDTO);
        return Ref.ok();
    }

    @SysLog("新增金刚位及icon图标")
    @ApiOperation("新增金刚位及icon图标")
    @RequestMapping("/addIcon")
    @RequiresPermissions("business:class:addIcon")
    public CommonResult<Void> addIcon(@RequestBody IconDTO iconDTO){
        shoppingUserClassService.addIcon(iconDTO);
        return Ref.ok();
    }

    @ApiOperation("上传icon图标到服务器")
    @RequestMapping("/uploadIcon")
    public CommonResult<List<UploadItem.UploadV0>> uploadIcon(HttpServletRequest request){
        if(!(request instanceof MultipartHttpServletRequest)){
            return Ref.error("no file upload");
        }
        MultipartHttpServletRequest req = (MultipartHttpServletRequest)request;
        Iterator<String> fileNames = req.getFileNames();
        List<UploadItem.UploadV0> items = new ArrayList<>();
        OSSClientUtil ossClientUtil;
        while (fileNames.hasNext()){
            String name = fileNames.next();
            List<MultipartFile> files = req.getFiles("file");
            if(files==null || files.size()==0){
                return Ref.error("param:"+name+",no file upload");
            }
            UploadItem.UploadV0 item = new UploadItem.UploadV0();
            for(MultipartFile file : files){
               if(file.getSize() <= 0){
                  continue;
               }
            ossClientUtil = new OSSClientUtil();
               try {
                   String icon = ossClientUtil.uploadImg2Oss(file,"icon");
                   if ("fail".equals(icon))
                        return Ref.error("上传失败");
                   String url = uploadHost+"icon/"+icon;
                   item.setName(name);
                   item.getUrlList().add(url);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            items.add(item);
        }
        return Ref.ok(items);
    }

}
